const fs = require("fs");
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const tee = require("@babel/types");
const generator = require("@babel/generator").default;

var cbbiYhhName = "cbbiYhh"

cbbiyhh = `
window.llp = 0
window.bhbh = []; 
function start(){
    window.bhbh = []; 
    window.llp = 1;
};

function pushBhbh(){
	window.llp = 0; 
	window.bhbh = [];
};
function ${cbbiYhhName}(yhh, cbb){
    if (window.llp == 0){
        return yhh;
    };
    let yhhi = typeof yhh;
    bhbh.push(yhhi+"---"+cbb);
    if (cbb != undefined){
    };
    return yhh;
};

//----------------------------------------分割线-----------------------------------------------------------------\n\r
`


function allToAst(text) {
    var hanHao = 0;
    var ast = parser.parse(text, {
        sourceType: "module"
    });
    traverse(ast, {
        AssignmentExpression(path) {
            hanHao += 1;
            if (tee.isIdentifier(path.node.left) || tee.isMemberExpression(path.node.left)) {
            } else {
                return 0;
            }
            ;

            try {
             cbbiyhhBlockList = [
                path.node.right,
                tee.StringLiteral('' + hanHao)
            ];
                path.node.right = tee.callExpression(tee.Identifier(cbbiYhhName), cbbiyhhBlockList);
                // path.skip();
            } catch (r) {
            }
        },
        VariableDeclarator(path) {
            hanHao += 1;
            cbbiyhhBlockList = [
                path.node.id,
                tee.StringLiteral('' + hanHao)

            ];
            try {
                path2 = path.parentPath;
                if (path2.isVariableDeclaration()) {
                    path2.insertAfter(tee.ExpressionStatement(tee.CallExpression(tee.Identifier(cbbiYhhName), cbbiyhhBlockList)));
                }
            } catch (r) {
            }
        }
    });
    astText = cbbiyhh + generator(ast, {
        minified: false,
        jsescOption: {
            minimal: true
        }
    }).code
    return astText
}


exports.allToAst = allToAst;
